//
//  DetailViewController.m
//  作业tableView通讯录
//
//  Created by dllo on 16/6/17.
//  Copyright © 2016年 zuzhiming. All rights reserved.
//

#import "DetailViewController.h"
#import "DetailsInfoView.h"
#import "CutomCell.h"
@interface DetailViewController ()<UITextFieldDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property (nonatomic, retain) UIImageView *userImageView;
@property (nonatomic, retain) UIImagePickerController *imagePickerController;
@property (nonatomic, retain) DetailsInfoView *detailsInfoView;

@end

@implementation DetailViewController

- (void)dealloc
{
    [_userImageView release];
    [_imagePickerController release];
    [_detailsInfoView release];
    [_dict release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self config];
    
    [self createSubViews];
}


- (void)config
{
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)createSubViews
{
    NSArray *array = @[@"姓名:", @"电话:", @"QQ:"];
    for (NSInteger i = 0; i < 3; i++) {
        self.detailsInfoView = [[DetailsInfoView alloc] initWithFrame:CGRectMake(30, i * 80 + 250, 300, 50) withName:[array objectAtIndex:i]];
        
        //content(是文本对象)
        self.detailsInfoView.content.delegate = self;
        [self.view addSubview:_detailsInfoView];
        [_detailsInfoView release];
    }
    
    self.userImageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 50, 175, 175)];
    //前一页的数据过来之后赋值
    self.userImageView.image = [UIImage imageNamed:[_dict objectForKey:@"photo"]];
    //修圆角
    self.userImageView.layer.cornerRadius = CGRectGetWidth(self.userImageView.bounds) / 2;
    self.userImageView.layer.masksToBounds = YES;
    //打开用户交互
    self.userImageView.userInteractionEnabled = YES;
    
    [self.view addSubview:_userImageView];
    [_userImageView release];
    
    
    //创建轻拍手势对象
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.userImageView addGestureRecognizer:tapGesture];
    [tapGesture release];
    
    
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(didClickedRightBarButton:)];
    
    self.navigationItem.rightBarButtonItem = rightBarButton;
    
}

- (void)didClickedRightBarButton:(UIBarButtonItem *)rightBarButton
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


//轻拍事件
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    self.imagePickerController = [[UIImagePickerController alloc] init];
    self.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    self.imagePickerController.allowsEditing = YES;
    self.imagePickerController.delegate = self;
    [self presentViewController:self.imagePickerController animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    NSLog(@"%@", info);
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    [self.userImageView setImage: image];
    [self dismissViewControllerAnimated:YES completion:nil];
}

//点击键盘返回按钮的时候出发事件-->回收键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    //失去第一响应者
    [textField resignFirstResponder];
    
    return YES;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    
        self.userImageView.frame = CGRectMake(135, 25, 175 / 2, 175 / 2);
    
        self.userImageView.layer.cornerRadius = CGRectGetWidth(self.userImageView.bounds) / 2;
        self.userImageView.clipsToBounds = YES;
    

   
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    
    self.userImageView.frame = CGRectMake(100, 50, 175, 175);
    
    self.userImageView.layer.cornerRadius = CGRectGetWidth(self.userImageView.bounds) / 2;
    self.userImageView.clipsToBounds = YES;
    return YES;
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    
}



@end
